//
//  FeedBackVC.m
//  New_Runway
//
//  Created by mack on 2018/3/28.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "FeedbackVC.h"
#import "NRWFeedBackApi.h"

@interface FeedBackVC ()<UITextViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@property (weak, nonatomic) IBOutlet UITextView *textview;
@property (weak, nonatomic) IBOutlet UILabel *wordNum;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (nonatomic, copy) NSString *contet;
@property (nonatomic, strong) NSData *data;
@property (weak, nonatomic) IBOutlet UITextField *PhoneNum;
@property (nonatomic, strong) NRWFeedBackApi *feedBackApi;
@property (nonatomic, copy) NSString *feedBackPhone;
@end

@implementation FeedBackVC
-(NRWFeedBackApi *)feedBackApi{
    if (!_feedBackApi) {
        _feedBackApi = [[NRWFeedBackApi alloc] init];
        _feedBackApi.delegate = self;
    }
    return _feedBackApi;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.contet = @"";
    self.data = [NSData data];
    [self.navigationView setTitle:@"意见反馈"];
    self.view.backgroundColor = backColor();
    self.topHeight.constant = getNavHeight() + 20;
    self.textview.tintColor = mainColor();
    self.textview.layer.masksToBounds = YES;
    self.textview.layer.borderWidth = 0.5f;
    self.textview.layer.borderColor = lineColor().CGColor;
    self.submitBtn.layer.masksToBounds = YES;
    self.submitBtn.layer.cornerRadius = 35/2;
    self.textview.delegate = self;
    self.PhoneNum.layer.masksToBounds = YES;
    self.PhoneNum.layer.borderWidth = 0.5;
    self.PhoneNum.layer.borderColor = lineColor().CGColor;
    self.PhoneNum.tintColor = mainColor();
    self.PhoneNum.delegate = self;
    self.PhoneNum.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 0)];
    self.PhoneNum.leftView.userInteractionEnabled = NO;
    self.PhoneNum.leftViewMode = UITextFieldViewModeAlways;
    
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    
    if([textView.text isEqualToString:@"请输入反馈内容"]){
        textView.text = @"";
        self.contet = @"";
    }
    
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length == 0) {
        textView.text = @"请输入反馈内容";
    }
}
-(void)textViewDidChange:(UITextView *)textView{
    
    if ([textView.text length] > 50) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 50)];
        [textView.undoManager removeAllActions];
        [textView becomeFirstResponder];
        return;
    }
    
    self.contet = textView.text;
    
    self.wordNum.text = [NSString stringWithFormat:@"(%lu/50)",(unsigned long)textView.text.length];
    
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    self.feedBackPhone = text;
    
    return YES;
}

- (IBAction)submitFeedBack:(id)sender {
    if (isEmptyString(self.contet) == YES) {
        [NRWMessageHUD showMessage:@"请输入反馈内容" forTime:2.0 withRemindId:remindWarning];
        return;
    }
    if (isEmptyString(self.feedBackPhone) == YES) {
        [NRWMessageHUD showMessage:@"请输入手机号" forTime:2.0 withRemindId:remindWarning];
        return;
    }
    if ([DataCheck valiMobile:self.feedBackPhone] == NO) {
        [NRWMessageHUD showMessage:@"请输入正确手机号" forTime:2.0 withRemindId:remindWarning];
        return;
    }
    [SVProgressHUD show];
    
    self.feedBackApi.feedbackPhone = self.feedBackPhone;
    self.feedBackApi.feedbackContent = self.contet;
    [self.feedBackApi start];
}
- (void)requestFinished:(__kindof YTKBaseRequest *)request {
    if (request == self.feedBackApi) {
        if ([request.responseObject[@"code"] isEqual:@1]) {
            [NRWMessageHUD showMessage:@"提交成功" forTime:2.0 withRemindId:remindSuccess];
            [self.navigationController popViewControllerAnimated:YES];

        }else{
            [NRWMessageHUD showMessage:@"提交失败" forTime:2.0 withRemindId:remindFail];

        }
    }
    [SVProgressHUD dismiss];

}

- (void)requestFailed:(__kindof YTKBaseRequest *)request {
    [SVProgressHUD dismiss];
    [NRWMessageHUD showMessage:@"提交失败" forTime:2.0 withRemindId:remindFail];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.textview resignFirstResponder];
    [self.PhoneNum resignFirstResponder];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
